package biz

import (
	"encoding/json"
	"github.com/obs-data-process/internal/biz/domain"
	"github.com/obs-data-process/internal/pkg/config"
	"github.com/pkg/errors"
	"io/ioutil"
)

type CommandUserCase struct {
	cfg *config.CommandConfig
}

func NewCommandUserCase(cfg *config.CommandConfig) *CommandUserCase {
	return &CommandUserCase{
		cfg: cfg,
	}
}

func (uc *CommandUserCase) GetCommands(businessType domain.BusinessType) ([]domain.Command, error) {
	if businessType == domain.HotelBusinessType {
		return loadProcessCommands(uc.cfg.HotelCommandConfigFile)
	}
	return loadProcessCommands(uc.cfg.MallCommandConfigFile)
}

func loadProcessCommands(filename string) ([]domain.Command, error) {
	bytes, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, errors.Wrap(err, "read command config file failed")
	}
	commands := make([]domain.Command, 0)
	err = json.Unmarshal(bytes, &commands)
	if err != nil {
		return nil, errors.Wrap(err, " json unmarshal failed")
	}
	return commands, nil
}
